/*
Copyright (C) 2008 Matthew Perry

This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This libary/program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using BaseTools.Searching;

namespace BaseTools.Configuration
{
	/// <summary>
	/// A simplified alternative to ConfigList
	/// </summary>
	[Obsolete("There is little use for this class")]
	public class PropertyList : ICloneable, IEnumerable<ConfigPair>
	{
		private ConfigList properties;
		
		/// <summary>
		/// Gets the enumerator of this PropertyList
		/// </summary>
		/// <returns>Enumerator</returns>
		public System.Collections.Generic.IEnumerator<ConfigPair> GetEnumerator()
		{
			return properties.GetEnumerator();
		}
		
		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}
		
		/// <summary>
		/// Creates a new PropertyList
		/// </summary>
		public PropertyList()
		{
			this.properties = new ConfigList();
		}
		
		/// <summary>
		/// Wraps a PropertyList around a ConfigList
		/// </summary>
		/// <param name="properties">The ConfigList to wrap around</param>
		public PropertyList(ConfigList properties)
		{
			this.properties = properties;
		}
		
		/// <summary>
		/// Creates a copy of the PropertyList
		/// </summary>
		/// <param name="other">The PropertyList to copy</param>
		public PropertyList(PropertyList other)
		{
			this.properties = (ConfigList)other.properties.Clone();
		}
		
		/// <summary>
		/// Creates a clone of this PropertyList
		/// </summary>
		/// <returns>A clone of this PropertyList</returns>
		public Object Clone()
		{
			return new PropertyList(this);
		}
		
		/// <summary>
		/// Sets a property or creates it if it does not exist
		/// </summary>
		/// <param name="name">The name of this property</param>
		/// <param name="value">The new value of the property</param>
		public void SetProperty(string name, string value)
		{
			ConfigPair p = properties.FindPair(new StringValidator(name, false));

			if (p == null)
			{
				properties.AddPair(new ConfigPair(name, value));
			}
			else
			{
				p.Data = value;
			}
		}
		
		/// <summary>
		/// Gets the value of a property
		/// </summary>
		/// <param name="name">The name of the property</param>
		/// <returns>The value of the property or null if it doesn't exist</returns>
		public string GetProperty(string name)
		{
			return properties.GetString(new StringValidator(name, false));
		}
		
		/**
		 * Deletes a property
		 * @param name the name of the property to delete
		 */
		public void DeleteProperty(string name)
		{
			properties.RemoveKey(new StringValidator(name, false));
		}
		
		/// <summary>
		/// Checks that a property exists
		/// </summary>
		/// <param name="name">The name of the property</param>
		/// <returns>Whether the property exists</returns>
		public bool HasProperty(string name)
		{
			return properties.HasString(new StringValidator(name, false));
		}
		
		/// <summary>
		/// Get/SetProperty operator
		/// </summary>
		public string this[string name]
		{
			get
			{
				return GetProperty(name);
			}
			set
			{
				SetProperty(name, value);
			}
		}
	}
}
